package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SysUserFbwz;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SysUserFbwzMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISysUserFbwzService;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 审评查验-用户发表文章
 * @Author: yuncheng
 * @Date: 2023-10-09
 * @Version: V1.0
 */
@Service
public class SysUserFbwzServiceImpl extends BaseServiceImpl<SysUserFbwzMapper, SysUserFbwz> implements ISysUserFbwzService {

    @Resource
    private SysUserFbwzMapper sysUserFbwzMapper;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Override
    public IPage<SysUserFbwz> selectPageList(SysUserFbwz query, PageVo pageVo, OrderItemVo orderItemVo) {
        LambdaQueryWrapper<SysUserFbwz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFqklb()), SysUserFbwz::getFqklb, query.getFqklb());
        queryWrapper.like(StringUtils.isNotBlank(query.getFqkmc()), SysUserFbwz::getFqkmc, query.getFqkmc());
        queryWrapper.like(StringUtils.isNotBlank(query.getFwzmc()), SysUserFbwz::getFwzmc, query.getFwzmc());
        queryWrapper.eq(StringUtils.isNotBlank(query.getFuserid()), SysUserFbwz::getFuserid, query.getFuserid());

        if (StringUtils.isBlank(orderItemVo.getOrderItem())){
            queryWrapper.orderByDesc(SysUserFbwz::getCreateTime);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        Page<SysUserFbwz> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        return sysUserFbwzMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SysUserFbwz> selectAllList(SysUserFbwz query) {
        LambdaQueryWrapper<SysUserFbwz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFqklb()), SysUserFbwz::getFqklb, query.getFqklb());
        queryWrapper.like(StringUtils.isNotBlank(query.getFqkmc()), SysUserFbwz::getFqkmc, query.getFqkmc());
        queryWrapper.like(StringUtils.isNotBlank(query.getFwzmc()), SysUserFbwz::getFwzmc, query.getFwzmc());
        queryWrapper.eq(StringUtils.isNotBlank(query.getFuserid()), SysUserFbwz::getFuserid, query.getFuserid());
        queryWrapper.orderByDesc(SysUserFbwz::getCreateTime);
        return sysUserFbwzMapper.selectList(queryWrapper);
    }

    @Override
    public SysUserFbwz addFbwz(SysUserFbwz bean) {
        bean.setId(GetUuIdUtils.ReplaceUuId());
        bean.setCreateTime(new Date());
        bean.setUpdateTime(new Date());
        bean.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        sysUserFbwzMapper.insert(bean);

        String operContent = "新增信息：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_SYS_USER_FBWZ,operContent,bean.getId(), "添加发表文章", SpcyConstant.LOGS_SYS_TYPE_XZ);

        return bean;
    }

    @Override
    public void updatwFbwz(SysUserFbwz buffer) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        if (StringUtils.isEmpty(buffer.getId())){
            throw new BusinessException("参数为空!");
        }
        SysUserFbwz updateBean = sysUserFbwzMapper.selectById(buffer.getId());
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, BeanUtils.describe(buffer));
        if (buffer.getFfbsj() != null){
            updateBean.setFfbsj(buffer.getFfbsj());
        }else {
            updateBean.setFfbsj(null);
        }
        updateBean.setUpdateTime(new Date());
        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        sysUserFbwzMapper.updateById(updateBean);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_SYS_USER_FBWZ,operContent,buffer.getId(),"修改发表文章",SpcyConstant.LOGS_SYS_TYPE_XG);
    }

    @Override
    public void updatwFbwzById(SysUserFbwz buffer) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        if (StringUtils.isEmpty(buffer.getId())){
            throw new BusinessException("参数为空!");
        }

        SysUserFbwz updateBean = sysUserFbwzMapper.selectById(buffer.getId());
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, BeanUtils.describe(buffer));
        if (buffer.getFfbsj() != null){
            updateBean.setFfbsj(buffer.getFfbsj());
        }else {
            updateBean.setFfbsj(null);
        }
        sysUserFbwzMapper.updateById(updateBean);
    }

    @Override
    public void deleteFbwz(String ids) {
        if (StringUtils.isEmpty(ids)){
            throw new BusinessException("参数为空!");
        }
        List<String> idsList = StringPatternUtils.getListByIds(ids);
        List<SysUserFbwz> list = sysUserFbwzMapper.selectBatchIds(idsList);
        if (CollectionUtil.isNotEmpty(list)){
            for (SysUserFbwz fbwz : list){
                sysUserFbwzMapper.deleteById(fbwz.getId());

                String operContent = "删除信息：";
                operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(fbwz));
                spcyLogService.addLogs(LogConstant.LOGS_SYS_USER_FBWZ,operContent,fbwz.getId(), "删除发表文章", SpcyConstant.LOGS_SYS_TYPE_SC);
            }
        }

    }
}
